package com.yuanmancong.redis.test.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.yuanmancong.redis.test.bean.Cart;

@Controller
public class CartController {

	@SuppressWarnings("rawtypes")
	@Resource
	RedisTemplate redisTemplate;
	
	@RequestMapping("list")
	public String list(Model model,@RequestParam(defaultValue = "1")Integer pageNum,
			@RequestParam(defaultValue = "10")Integer pageSize) {
		ListOperations<String,Cart> opsForList = redisTemplate.opsForList();
		List<Cart> list = opsForList.range("cart", (pageNum-1)*pageSize, pageNum*pageSize -1);
		Long size = opsForList.size("cart");
		Page<Cart> page = new Page<Cart>(pageNum, pageSize);
		page.setTotal(size);
		PageInfo<Cart> info = new PageInfo<Cart>(page);
		info.setList(list);
		model.addAttribute("info", info);
		return "list";
	}
	
}
